import { accountLogin,} from '../services/api';
import { getAuthority,setAuthority } from '../utils/authority';

export default {
  namespace: 'user',

  state: {
    data: {},
    loading:true,
    isLogin:false,
  },

  effects: {
    *loadUserInfo({ payload }, { call, put }){
      const response = yield call(getAuthority);
      console.log(response);
      yield put({
        type: 'setUserInfo',
        payload: response,
      });
    },
    *login({ payload }, { call, put }) {
      const response = yield call(accountLogin,payload);
      console.log(response);
      yield put({
        type: 'setUserInfo',
        payload: response.data,
      });
    },
  },
  reducers: {
    setUserInfo(state, action) {
      console.log(action);
      if(action.payload){
        return {
          ...state,
          data: action.payload,
          isLogin:true,
          loading:false,
        };
      }
      return {
        ...state,
        isLogin:false,
        loading:false,
      };
      
    },
    logOut(state, action) {
      return {
        ...state,
        isLogin:false
      };
    },
  },
};
